<template>
  <div class="lb-tag">
    <top-nav></top-nav>
    <div class="page-main">
      <lb-button type="primary" icon="el-icon-plus" @click="openDialog"
        >添加印象标签</lb-button
      >
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="tag" label="标签"> </el-table-column>
        <el-table-column prop="create_time" label="加入时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time.split(' ')[0] }}</p>
            <p>{{ scope.row.create_time.split(' ')[1] }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column label="是否上架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateOrDelTag(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="openDialog(scope.row)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="updateOrDelTag(scope.row.id, 2)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
        <lb-button>设置默认{{ $t('menu.BusinessCard') }}</lb-button>
      </lb-page>
    </div>
    <el-dialog
      :title="dialogForm.id ? '编辑印象标签' : '添加印象标签'"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form
        @submit.native.prevent
        label-width="80px"
        :model="dialogForm"
        :rules="dialogFormRules"
        ref="dialogForm"
      >
        <el-form-item label="标签" prop="name">
          <el-input
            v-model="dialogForm.name"
            placeholder="请输入标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input
            v-model.number="dialogForm.top"
            placeholder="请输入排序值"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTag">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      centerDialogVisible: false,
      loading: false,
      dialogForm: {
        id: '',
        name: '',
        top: 0
      },
      dialogFormRules: {
        name: {
          required: true,
          type: 'string',
          message: '请输入标签',
          trigger: 'blur'
        },
        top: {
          required: true,
          type: 'number',
          message: '请输入排序值',
          trigger: 'blur'
        }
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  activated () {
    this.getTagList()
  },
  methods: {
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTagList()
    },
    /**
     * @method 上下架、删除标签
     */
    updateOrDelTag (id, status) {
      let obj = { id, method: status }
      if (status === 2) {
        this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
          confirmButtonText: this.$t('action.comfirm'),
          cancelButtonText: this.$t('action.cancel'),
          type: 'warning'
        }).then(() => {
          this.handleUpdateTag(obj)
        })
      } else {
        this.handleUpdateTag(obj)
      }
    },
    /**
     * @method 上下架、删除标签接口处理
     */
    handleUpdateTag (query) {
      this.$api.updateTag(query).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t(query.method === 2 ? 'tips.successDel' : 'tips.successOper'))
          if (query.method === 2) {
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTagList()
          }
        } else {
          if (query.method === 2) return
          this.getTagList()
        }
      })
    },
    /**
     * @method 获取标签列表
     */
    getTagList () {
      this.loading = true
      let { page, limit } = this
      this.$api.getTagList({ page, limit }).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 打开标签输入框
     */
    openDialog (item = {}) {
      for (let key in this.dialogForm) {
        this.dialogForm[key] = item[key]
      }
      if (item.id) {
        this.dialogForm.name = item.tag
      }
      this.centerDialogVisible = !this.centerDialogVisible
    },
    /**
     * @method 添加标签
     */
    async addTag () {
      this.$refs['dialogForm'].validate(valid => {
        if (!valid) {
          return false
        } else {
          this.$api.editOrAddTag(this.dialogForm).then(res => {
            if (res.code === 200) {
              this.centerDialogVisible = false
              this.getTagList()
              this.$message({
                type: 'success',
                message: this.$t('tips.successSub')
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-tag {
  width: 100%;
  .page-main {
    .el-table {
      margin-top: 24px;
    }
  }
}
</style>
